package icu.qx.qc.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.api.domain.SysUser;
import icu.qx.qc.domain.QcProQuality;
import icu.qx.qc.domain.QcQualityProject;
import icu.qx.qc.domain.param.QcProQualityParam;
import icu.qx.qc.feign.ProWorkOrderService;
import icu.qx.qc.feign.ProWorkTaskService;
import icu.qx.qc.feign.SysUserFeign;
import icu.qx.qc.service.IQcQualityQprojectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import icu.qx.qc.service.IQcProQualityService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 过程检验Controller
 *
 * @author yong
 * @date 2024-06-24
 */
@RestController
@RequestMapping("/qc/proQuality")
public class QcProQualityController extends BaseController {
    @Autowired
    private IQcProQualityService qcProQualityService;
    @Autowired
    private ProWorkOrderService proWorkOrderService;
    @Autowired
    private ProWorkTaskService proWorkTaskService;
    @Autowired
    private SysUserFeign sysUserFeign;
    @Autowired
    private IQcQualityQprojectService qcQualityQprojectService;

    /**
     * 查询过程检验列表
     */
    @RequiresPermissions("qc:proQuality:list")
    @GetMapping("/list")
    public TableDataInfo list(QcProQuality qcProQuality) {
        startPage();
        List<QcProQuality> list = qcProQualityService.selectQcProQualityList(qcProQuality);
        return getDataTable(list);
    }

    /**
     * 导出过程检验列表
     */
    @RequiresPermissions("qc:proQuality:export")
    @Log(title = "过程检验", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QcProQuality qcProQuality) {
        List<QcProQuality> list = qcProQualityService.selectQcProQualityList(qcProQuality);
        ExcelUtil<QcProQuality> util = new ExcelUtil<QcProQuality>(QcProQuality.class);
        util.exportExcel(response, list, "过程检验数据");
    }

    /**
     * 获取过程检验详细信息
     */
    @RequiresPermissions("qc:proQuality:query")
    @GetMapping(value = "/qcCode/{reportQcCode}")
    public AjaxResult getInfoByQcCode(@PathVariable("reportQcCode") String reportQcCode) {
        QcProQuality qcProQuality = qcProQualityService.selectQcProQualityByQcCode(reportQcCode);
        AjaxResult ajaxResult = success(qcProQuality);
        AjaxResult r1 = proWorkOrderService.getInfo(qcProQuality.getReportOCode());
        AjaxResult r2 = proWorkTaskService.getInfo(qcProQuality.getReportTCode());
        AjaxResult r3 = sysUserFeign.getInfo(qcProQuality.getReportQcPeo());
        R<SysUser> r4 = sysUserFeign.username(qcProQuality.getReportPeoCode());
        qcProQuality.setReportPeoName(r4.getData().getUserName());
        List<QcQualityProject> qcQualityProjects = qcQualityQprojectService.selectQcQualityQprojectByQqProCheckCode(qcProQuality.getReportQcCode());
        ajaxResult.put("selectedProjectList", qcQualityProjects);
        ajaxResult.put("selectedGood", qcProQuality.getGbsGood());
        ajaxResult.put("selectedWorkOrder", r1.get("data"));
        ajaxResult.put("selectedOrderTask", r2.get("data"));
        ajaxResult.put("selectedQcPeo", r3.get("data"));
        return ajaxResult;
    }

    /**
     * 新增过程检验
     */
    @RequiresPermissions("qc:proQuality:add")
    @Log(title = "过程检验", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QcProQualityParam qcProQualityParam) {
        return toAjax(qcProQualityService.insertQcProQuality(qcProQualityParam));
    }

    /**
     * 修改过程检验
     */
    @RequiresPermissions("qc:proQuality:edit")
    @Log(title = "过程检验", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QcProQualityParam qcProQualityParam) {
        return toAjax(qcProQualityService.updateQcProQuality(qcProQualityParam));
    }

    /**
     * 删除过程检验
     */
    @RequiresPermissions("qc:proQuality:remove")
    @Log(title = "过程检验", businessType = BusinessType.DELETE)
    @DeleteMapping("/{reportCodes}")
    public AjaxResult remove(@PathVariable String[] reportCodes) {
        return toAjax(qcProQualityService.deleteQcProQualityByReportCodes(reportCodes));
    }
}
